package com.wen.update;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.support.v4.app.NotificationCompat;

import java.util.UUID;

/**
 * 通知栏管理类
 */

public class NotifyManager {
    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    private static final int NOTIFICATION_ID = UUID.randomUUID().hashCode();
    private Context mContext;
    private String mVersionName;

    public NotifyManager(Context context, String versionName) {
        mContext = context;
        mVersionName = versionName;
        mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(mContext);
        initNotify();
    }

    /**
     * 1. 初始化通知栏
     */
    private void initNotify() {
        mBuilder.setContentTitle(String.format("%s  %s", mContext.getString(mContext.getApplicationInfo().labelRes), mVersionName))
                //通知首次出现在通知栏，带上升动画效果的
                .setTicker("爱车小屋")
                //通常是用来表示一个后台任务
                .setOngoing(true)
                //通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                .setWhen(System.currentTimeMillis());

        //解决5.0系统通知栏白色Icon的问题
        Drawable appIcon = getAppIcon(mContext);
        Bitmap drawableToBitmap = null;
        if (appIcon != null) {
            drawableToBitmap = drawableToBitmap(appIcon);
        }
        if (drawableToBitmap != null) {
            mBuilder.setSmallIcon(mContext.getApplicationInfo().icon);
            mBuilder.setLargeIcon(drawableToBitmap);
        } else {
            mBuilder.setSmallIcon(mContext.getApplicationInfo().icon);
        }
    }

    /**
     * 更新通知栏的进度(下载中)
     *
     * @param progress
     */
    public void updateProgress(int progress) {
        mBuilder.setContentText(String.format("正在下载:%1$d%%", progress)).setProgress(100, progress, false);
        PendingIntent pendingintent = PendingIntent.getActivity(mContext, 0, new Intent(), PendingIntent
                .FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingintent);
        mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

    /**
     * 合成更新的Icon
     */
    public Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    /**
     * 获取App的Icon
     */
    public Drawable getAppIcon(Context context) {
        try {
            return context.getPackageManager().getApplicationIcon(context.getPackageName());
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 移除通知
     */
    public void cancel() {
        // 移除通知
        if (mNotifyManager != null) {
            mNotifyManager.cancel(NOTIFICATION_ID);
        }
    }
}

